package de.bloxel.controller;

import de.bloxel.core.events.Event;
import de.bloxel.core.events.EventListener;
import de.lessvoid.nifty.Nifty;

/**
 * encapsulates functionality to switch between different UI screens
 * @author dorer
 *
 *TODO this should be more data-driven: UIManager gets a map which UIEvent.Action maps to which screen
 *TODO to support more types of screens the UIManager should remember the last screen it switched from
 *to enable toggling back to it from the menu
 */
public class UIManager implements EventListener {

  private final Nifty niftyUI;
  private String lastScreenId;

  public UIManager(final Nifty nifty) {
    niftyUI=nifty;
  }

  @Override
  public boolean handleEvent(final Event theEvent) {
    if (theEvent.getType().equalsIgnoreCase("UIEvent")) {
      final UIEvent uiEvent=(UIEvent) theEvent;
      if (uiEvent.getAction()==UIEvent.Action.ToggleMenu) {
        toggleMenu();
        return true;
      }
    }

    return false;
  }

  public void initialize() {
    niftyUI.gotoScreen("hud");
  }

  /**
   * switches between main menu and game
   */
  private void toggleMenu() {
    // TODO Auto-generated method stub
    if (niftyUI.getCurrentScreen()==null || !niftyUI.getCurrentScreen().getScreenId().equalsIgnoreCase("menu")) {
      lastScreenId=(niftyUI.getCurrentScreen()==null)?"":niftyUI.getCurrentScreen().getScreenId();
      niftyUI.gotoScreen("menu");
      return;
    }
    if (niftyUI.getCurrentScreen().getScreenId().equalsIgnoreCase("menu")) {
      niftyUI.gotoScreen(lastScreenId);
      lastScreenId="menu";
      return;
    }

  }

}
